<?php
/**
 * Created by PhpStorm.
 * User: 郝飞
 * Date: 2017/6/29
 * Time: 14:41
 */

namespace Publish\Controller;
use Common\Controller\AdminbaseController;

class AdminEntryController extends AdminbaseController{
	protected $dictionary_model;

	function _initialize() {
		parent::_initialize();
		$this->terms_model = D("Publish/SystemTerms");
		$this->dictionary_model = D('Publish/PubDictionary');
	}

	function index(){
		//词条内容
		$entry_info = $this->dictionary_model->order('id desc,createtime desc')->select();
		$this->assign('list',$entry_info);
		$this->display();
	}

	/*删除*/
	function delete(){
		$id = I('id');
		$is_del = $this->dictionary_model->delete($id);
		if($is_del !== false){
			$this->success("删除成功！");
		}else{
			$this->error("删除失败！");
		}
	}

	function add(){
		$this->_getTermTree();
		$this->display('from');
	}

	function edit(){
		$id = I('id');
		$info = $this->dictionary_model->find($id);
		$this->_getTree($info['parameters']);
		$this->assign($info);
		$this->display('from');
	}

	function data_post(){
		if (IS_POST) {
			$id = I('id');
			$term_id = I("term", 0, 'intval'); //分类id
			if(empty($term_id['0']) || $term_id['0'] == 0) {
				$data['parameters'] = I('parameters');
			}else{ //人物标识id
				$data['parameters'] = $term_id[0];
			}
			$data['identification'] = I('identification'); //词条标识 0人物 1分类
			$data['dicname'] = I('dicname'); //词条内容
			$data['content'] = htmlspecialchars_decode(I('content'));//词条文章
			$data['createtime'] = time();
			if($id){
				$is_edit = $this->dictionary_model->where("id=$id")->save($data);
				if ($is_edit !== false) {
					$this->success("修改成功！",U("AdminEntry/index"));
				} else {
					$this->error("修改失败！");
				}
			}else{
				if ($this->dictionary_model->add($data)!==false) {
					$this->success("添加成功！",U("AdminEntry/index"));
				} else {
					$this->error("添加失败！");
				}
			}
		}
	}

	// 获取文章分类树结构 select 形式
	private function _getTree($trem_id=0)
	{
		$term_id = empty($trem_id) ? 0 : intval($trem_id);
		$result = $this->terms_model->order(array("listorder" => "asc"))->select();

		$tree = new \Tree();
		$tree->icon = array('&nbsp;&nbsp;&nbsp;│ ', '&nbsp;&nbsp;&nbsp;├─ ', '&nbsp;&nbsp;&nbsp;└─ ');
		$tree->nbsp = '&nbsp;&nbsp;&nbsp;';
		foreach ($result as $r) {
			$r['str_manage'] = '<a href="' . U("AdminTerm/add", array("parent" => $r['term_id'])) . '">添加子类</a> | <a href="' . U("AdminTerm/edit", array("id" => $r['term_id'])) . '">修改</a> | <a class="js-ajax-delete" href="' . U("AdminTerm/delete", array("id" => $r['term_id'])) . '">删除</a> ';
			$r['visit'] = "<a href='#'>访问</a>";
			$r['taxonomys'] = $this->taxonomys[$r['taxonomy']];
			$r['id'] = $r['term_id'];
			$r['parentid'] = $r['parent'];
			$r['selected'] = $term_id == $r['term_id'] ? "selected" : "";
			$array[] = $r;
		}

		$tree->init($array);
		$str = "<option value='\$id' \$selected >\$spacer\$name</option>";
		$taxonomys = $tree->get_tree(0, $str);
		$this->assign("taxonomys", $taxonomys);
	}

	// 获取文章分类树结构
	private function _getTermTree($term = array())
	{
		$result = $this->terms_model->order(array("listorder" => "asc"))->select();

		$tree = new \Tree();
		$tree->icon = array('&nbsp;&nbsp;&nbsp;│ ', '&nbsp;&nbsp;&nbsp;├─ ', '&nbsp;&nbsp;&nbsp;└─ ');
		$tree->nbsp = '&nbsp;&nbsp;&nbsp;';
		foreach ($result as $r) {
			$r['str_manage'] = '<a href="' . U("AdminTerm/add", array("parent" => $r['term_id'])) . '">添加子类</a> | <a href="' . U("AdminTerm/edit", array("id" => $r['term_id'])) . '">修改</a> | <a class="js-ajax-delete" href="' . U("AdminTerm/delete", array("id" => $r['term_id'])) . '">删除</a> ';
			$r['visit'] = "<a href='#'>访问</a>";
			$r['taxonomys'] = $this->taxonomys[$r['taxonomy']];
			$r['id'] = $r['term_id'];
			$r['parentid'] = $r['parent'];
			$r['selected'] = in_array($r['term_id'], $term) ? "selected" : "";
			$r['checked'] = in_array($r['term_id'], $term) ? "checked" : "";
			$array[] = $r;
		}

		$tree->init($array);
		$str = "<option value='\$id' \$selected >\$spacer\$name </option>";
		$taxonomys = $tree->get_tree(0, $str);
		$this->assign("taxonomys", $taxonomys);
	}



}